package com.yuanqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.entity.WareWarehouse;
import com.yuanqi.service.WareWarehouseService;
import com.yuanqi.mapper.WareWarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
* @author HP
* @description 针对表【ware_warehouse(仓库)】的数据库操作Service实现
* @createDate 2025-08-26 14:58:48
*/
@Service
public class WareWarehouseServiceImpl extends ServiceImpl<WareWarehouseMapper, WareWarehouse>
    implements WareWarehouseService{

    @Autowired
    private WareWarehouseMapper wareWarehouseMapper;


    @Override
    public IPage<WareWarehouse> listWarehouses(Integer page, Integer size, String name, String manager) {
        LambdaQueryWrapper<WareWarehouse> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(name)) {
            queryWrapper.like(WareWarehouse::getWhName, name);
        }

        if (StringUtils.hasText(manager)) {
            queryWrapper.like(WareWarehouse::getManager, manager);
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(WareWarehouse::getCreatedAt);

        // 分页查询
        return this.page(new Page<>(page, size), queryWrapper);
    }

    @Override
    public WareWarehouse getWarehouseById(Long id) {
        WareWarehouse wareWarehouse = this.getById(id);
        return wareWarehouse;
    }

    @Override
    //@Transactional
    public boolean saveWarehouse(WareWarehouse wareWarehouse) {
        // 设置创建时间和更新时间
        Date date = new Date();
        wareWarehouse.setCreatedAt(date);
        wareWarehouse.setUpdatedAt(date);

        return this.save(wareWarehouse);
    }

    @Override
    //@Transactional
    public boolean updateWarehouse(WareWarehouse wareWarehouse) {
        // 设置更新时间
        wareWarehouse.setUpdatedAt(new Date());

        return this.updateById(wareWarehouse);
    }

    @Override
    //@Transactional
    public boolean deleteWarehouse(Long id) {
        return this.removeById(id);
    }
}




